package main.iwith;

import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * Created by Administrator on 2018/9/25.
 */
public class NotRepeat {

    /**
     * 图谱去掉重复的商品名称
     * @param args
     */
    public static void main(String[] args){
        FileReader file;
        BufferedWriter writer = null;
        try {
            // 原有的知识图谱文件路径
            file = new FileReader("G:/SB.txt");

            BufferedReader br=new BufferedReader(file);

            Map<String,Object> map=new HashMap<>();
            String line ;
            while ((line = br.readLine())!=null){

                // 获取商品的名称
                String name=ForAnligla.getItemName(line);

                // 去掉商品中的数字
                if(Operator.HasDigit(name)){
                    for(int i = 0; i < name.length(); i++){
                        if(Operator.isDigit(name.charAt(i))){
                            name = name.substring(0, i);
                            break;
                        }
                    }
                }

                // 判断商品是否重复
                if(!map.containsKey(name)){
                    map.put(name, name);
                }else{
                    System.out.println(name + "  重复了");
                }
            }

            // 新生成的知识图谱，去掉重复商品名称的
            File file1 = new File("G:/sb_name.txt");
            if (file1.exists()) {
                file1.delete();
            }
            file1.createNewFile();

            writer = new BufferedWriter(new FileWriter(file1));

            Set<String> set = map.keySet();

            for(String str : set){
                // 写入文件的格式（可修改）
                writer.write("name:" + str.toString() + "\t" + "type:菜名" + "\t" + "entity:2" + "\t"
                        + "entityclass:/菜名" + "\t" + "alias:" + System.getProperty("line.separator"));
            }

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
